//
//  LJJWaterflowLayout.m
//  瀑布流
//
//  Created by 刘家俊 on 17/4/25.
//  Copyright © 2017年 Andrew. All rights reserved.
//

#import "LJJWaterflowLayout.h"

/** 默认的列数 */
static const NSInteger LJJDefaultColumnCount = 3;
/** 每一列之间的间距 */
static const CGFloat LJJDefaultColumnMargin = 10;
/** 每一行之间的间距 */
static const CGFloat LJJDefaultRowMargin = 10;
/** 边缘间距 */
static const UIEdgeInsets LJJDefaultEdgeInsets = {10, 10, 10, 10};

@interface LJJWaterflowLayout ()

/* 存放所有cell的布局属性 */
@property (nonatomic, strong) NSMutableArray *attrsArray;
/* 存放所有列的当前最高度 */
@property (nonatomic, strong) NSMutableArray *columnHeights;
/* 内容的高度 */
@property (nonatomic, assign) CGFloat contentHeight;

- (CGFloat)ljj_rowMargin;
- (CGFloat)ljj_columnMargin;
- (CGFloat)ljj_columnCount;
- (UIEdgeInsets)ljj_edgeInsets;

@end

@implementation LJJWaterflowLayout

#pragma mark - 边距数据处理
/** 每一行之间的间距 */
- (CGFloat)ljj_rowMargin
{
    if ([self.delegate respondsToSelector:@selector(ljj_rowMarginInWaterflowLayout:)]) {
        return [self.delegate ljj_rowMarginInWaterflowLayout:self];
    } else {
        return LJJDefaultRowMargin;
    }
}

/** 每一列之间的间距 */
- (CGFloat)ljj_columnMargin
{
    if ([self.delegate respondsToSelector:@selector(ljj_columnMarginInWaterflowLayout:)]) {
        return [self.delegate ljj_columnMarginInWaterflowLayout:self];
    } else {
        return LJJDefaultColumnMargin;
    }
}

/** 多少列 */
- (CGFloat)ljj_columnCount
{
    if ([self.delegate respondsToSelector:@selector(ljj_columnCountIndWaterflowLayout:)]) {
        return [self.delegate ljj_columnCountIndWaterflowLayout:self];
    } else {
        return LJJDefaultColumnCount;
    }
}

/** 边缘间距 */
- (UIEdgeInsets)ljj_edgeInsets
{
    if ([self.delegate respondsToSelector:@selector(ljj_edgeInsetsInWaterflowLayout:)]) {
        return [self.delegate ljj_edgeInsetsInWaterflowLayout:self];
    } else {
        return LJJDefaultEdgeInsets;
    }
}

#pragma mark - 懒加载
- (NSMutableArray *)columnHeights
{
    if (!_columnHeights) {
        _columnHeights = [NSMutableArray array];
    }
    return _columnHeights;
}

- (NSMutableArray *)attrsArray
{
    if (!_attrsArray) {
        _attrsArray = [NSMutableArray array];
    }
    return _attrsArray;
}

/**
 * 初始化
 */
- (void)prepareLayout
{
    [super prepareLayout];
    
    self.contentHeight = 0;
    
    //清除以前计算的所有高度
    [self.columnHeights removeAllObjects];
    for (NSInteger i = 0; i < self.ljj_columnCount; i++) {
        [self.columnHeights addObject:@(self.ljj_edgeInsets.top)];
    }
    
    //清除之前所有的布局属性
    [self.attrsArray removeAllObjects];
    
    //开始创建每一个cell对应的布局属性
    NSInteger count = [self.collectionView numberOfItemsInSection:0];
    
    for (NSInteger i = 0; i < count; i++) {
        //创建位置
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
        //获取indexPath位置cell对应的布局属性
        UICollectionViewLayoutAttributes *attrs = [self layoutAttributesForItemAtIndexPath:indexPath];
        [self.attrsArray addObject:attrs];
    }
}

/**
 * 决定cell的排布
 */
- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect
{
    return self.attrsArray;
}


/**
 返回indxpath位置cell对应的布局属性

 @param indexPath 对应的cell
 @return 布局约束
 */
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
    //创建布局属性
    UICollectionViewLayoutAttributes *attrs = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    
    //collectionView宽度
    CGFloat collectionViewW = self.collectionView.frame.size.width;
    
    //设置布局属性的frame
    CGFloat w = (collectionViewW - self.ljj_edgeInsets.left - self.ljj_edgeInsets.right - (self.ljj_columnCount - 1) * self.ljj_columnMargin) / self.ljj_columnCount;
    
    CGFloat h = [self.delegate ljj_waterflowLayout:self heightForItemAtIndex:indexPath.item itemWidth:w];
    
    //找出高度最短的那一列
    NSInteger destColumn = 0;
    CGFloat minColumnHeight = [self.columnHeights[0] doubleValue];
    for (NSInteger i = 0; i < self.ljj_columnCount; i ++) {
        //取得第i列的高度
        CGFloat columnHeight = [self.columnHeights[i] doubleValue];
        
        if (minColumnHeight > columnHeight) {
            minColumnHeight = columnHeight;
            destColumn = i;
        }
    }
    
    CGFloat x = self.ljj_edgeInsets.left + destColumn * (w + self.ljj_columnMargin);
    CGFloat y = minColumnHeight;
    if (y != self.ljj_edgeInsets.top) {
        y += self.ljj_rowMargin;
    }
    attrs.frame = CGRectMake(x, y, w, h);
    
    //更新最短那列的高度
    self.columnHeights[destColumn] = @(CGRectGetMaxY(attrs.frame));
    
    //记录内容的高度
    CGFloat columnHeight = [self.columnHeights[destColumn] doubleValue];
    if (self.contentHeight < columnHeight) {
        self.contentHeight = columnHeight;
    }
    
    return attrs;
}

- (CGSize)collectionViewContentSize
{
    return CGSizeMake(0, self.contentHeight + self.ljj_edgeInsets.bottom);
}


@end
